import { NextRequest, NextResponse } from 'next/server';
import { importAllDataFromCsv } from '@/lib/excel-to-sqlite-importer';

export async function POST(request: NextRequest) {
  try {
    console.log('Starting CSV data import process...');
    const result = await importAllDataFromCsv();
    
    if (result.success) {
        return NextResponse.json({
          success: true,
        message: result.message,
        importedCount: result.importedCount
        });
    } else {
        return NextResponse.json({
        success: false,
        error: result.message
      }, { status: 500 });
    }
  } catch (error) {
    const message = error instanceof Error ? error.message : 'An unknown error occurred';
    console.error(`Data import failed: ${message}`);
    return NextResponse.json({
      success: false,
      error: 'Failed to import data'
    }, { status: 500 });
  }
}

export async function GET() {
  try {
    return NextResponse.json({
      success: true,
      message: 'Import endpoint is ready'
    });
  } catch (error) {
    console.error('获取导入端点失败:', error);
    return NextResponse.json(
      { error: '获取导入端点失败' },
      { status: 500 }
    );
  }
} 